package net.cellkyborg.server;

import net.cellkyborg.RSInstances.RSNPC;
import java.io.*;

public class NPCHandler {
       
        public static int maxNPC = 10000;
        public static int maxListedNPCs = 10000;
	public RSNPC[] npcs = new RSNPC[maxNPC];

	public NPCHandler() {
		for (int i = 0; i < maxNPC; i++)
			npcs[i] = null;
                  loadAutoSpawn("/NPC_SPAWN.cfg");
	}

	private int getFreeSlot() {
		for (int i = 1; i < maxNPC; i++)
			if (npcs[i] == null)
				return i;
		return -1;
	}

	public boolean addNPC(RSNPC npc) {
		int slot = getFreeSlot();
		if (slot < 1)
			return false;
		npc.slot = slot;
		npcs[slot] = npc;
		return true;
	}
        public int GetNpcListHP(int NpcID) {
	return 0;	
	}
       public boolean loadAutoSpawn(String FileName) {
		String line = "";
		String token = "";
		String token2 = "";
		String token2_2 = "";
		String[] token3 = new String[10];
		boolean EndOfFile = false;
		BufferedReader characterfile = null;
		try {
			characterfile = new BufferedReader(new FileReader("./data/"+FileName));
		} catch(FileNotFoundException fileex) {
			misc.println(FileName+": file not found.");
			return false;
		}
		try {
			line = characterfile.readLine();
		} catch(IOException ioexception) {
			misc.println(FileName+": error loading file.");
			return false;
		}
		while(EndOfFile == false && line != null) {
			line = line.trim();
			int spot = line.indexOf("=");
			if (spot > -1) {
				token = line.substring(0, spot);
				token = token.trim();
				token2 = line.substring(spot + 1);
				token2 = token2.trim();
				token2_2 = token2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token2_2 = token2_2.replaceAll("\t\t", "\t");
				token3 = token2_2.split("\t");
				if (token.equals("spawn")) {
					newNPC(Integer.parseInt(token3[0]), Integer.parseInt(token3[1]), Integer.parseInt(token3[2]), Integer.parseInt(token3[3]), Integer.parseInt(token3[4]), Integer.parseInt(token3[5]), Integer.parseInt(token3[6]), Integer.parseInt(token3[7]), Integer.parseInt(token3[8]), GetNpcListHP(Integer.parseInt(token3[0])));
				}
			} else {
				if (line.equals("[ENDOFSPAWNLIST]")) {
					try { characterfile.close(); } catch(IOException ioexception) { }
					return true;
				}
			}
			try {
				line = characterfile.readLine();
			} catch(IOException ioexception1) { EndOfFile = true; }
		}
		try { characterfile.close(); } catch(IOException ioexception) { }
		return false;
	} 
        boolean invul;
        public boolean newNPC(int npcID, int x, int y, int heightLevel, int rangex1, int rangey1, int rangex2, int rangey2, int WalkingType, int HP) {
		int slot = getFreeSlot();
		if (slot < 1)
			return false;

		RSNPC npc = new RSNPC();
		npc.npcID = npcID;
		npc.location[0] = x;
		npc.location[1] = y;
		npc.location[2] = heightLevel;
		npc.invulnerable = invul;

		npc.slot = slot;

		npc.mapRegion[0] = x >> 3;
		npc.mapRegion[1] = y >> 3;
		npc.currentLocation[0] = x - (npc.mapRegion[0] - 6) * 8;
		npc.currentLocation[1] = y - (npc.mapRegion[1] - 6) * 8;

		npcs[slot] = npc;
		return true;
	}
	public boolean addNewNPC(int npcID, int x, int y, int h, boolean invul) {
		int slot = getFreeSlot();
		if (slot < 1)
			return false;

		RSNPC npc = new RSNPC();
		npc.npcID = npcID;
		npc.location[0] = x;
		npc.location[1] = y;
		npc.location[2] = h;
		npc.invulnerable = invul;

		npc.slot = slot;

		npc.mapRegion[0] = x >> 3;
		npc.mapRegion[1] = y >> 3;
		npc.currentLocation[0] = x - (npc.mapRegion[0] - 6) * 8;
		npc.currentLocation[1] = y - (npc.mapRegion[1] - 6) * 8;

		npcs[slot] = npc;
		return true;
	}

	public void process() {
		for (int i = 1; i < maxNPC; i++) {
			if (npcs[i] != null) {
				npcs[i].process();
				if (npcs[i].destroy) {
					npcs[i] = null;
				}
			}
		}
	}

	public void shutdown() {

	}
}

class NPCList {
	public int npcId;
	public String npcName;
	public int npcCombat;
	public int npcHealth;
	
	public NPCList(int _npcId) {
		npcId = _npcId;
	}
}